package com.example.toolbar.toolbar;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.example.toolbar.R;
import com.example.toolbar.databinding.ActivityToolbar1Binding;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

/***
 * 通过theme设置windowActionBar、windowNoTitle去掉application级别的theme，来达到不显示默认的materialdesign的标题名称
 */
public class Toolbar1Activity extends AppCompatActivity {

    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityToolbar1Binding binding = ActivityToolbar1Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mToolbar = binding.tbar;

        //设置NavigationIcon
        mToolbar.setNavigationIcon(R.mipmap.menu);
        //设置NavigationIcon button点击事件
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(Toolbar1Activity.this, "你点击了menu菜单", Toast.LENGTH_SHORT).show();
//                finish();
            }
        });
        //设置 ToolBar 背景色
        mToolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
        //设置标题
        mToolbar.setTitle("TitleTbar");
        //设置标题的文字颜色
        mToolbar.setTitleTextColor(Color.WHITE);
        //设置ToolBar的副标题
        mToolbar.setSubtitle("SubTitleTbar");
        //设置ToolBar的副标题 文字颜色
        mToolbar.setSubtitleTextColor(Color.RED);
        //设置logo
        mToolbar.setLogo(R.mipmap.ic_launcher);
        //拉近navigationIcon与logo的距离
        mToolbar.setContentInsetStartWithNavigation(0);

        //设置ToolBar menu
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.inflateMenu(R.menu.base_toolbar_menu);//设置右上角的填充菜单
        }
        //设置溢出菜单的图标
        mToolbar.setOverflowIcon(getResources().getDrawable(R.mipmap.menu));
        //设置menu item点击事件
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int menuItemId = item.getItemId();
                    if (menuItemId == R.id.action_search) {
                        Toast.makeText(Toolbar1Activity.this, "搜索", Toast.LENGTH_SHORT).show();
                    } else if (menuItemId == R.id.action_notification) {
                        Toast.makeText(Toolbar1Activity.this, "通知", Toast.LENGTH_SHORT).show();
                    } else if (menuItemId == R.id.action_item1) {
                        Toast.makeText(Toolbar1Activity.this, "item_01", Toast.LENGTH_SHORT).show();
                    } else if (menuItemId == R.id.action_item2) {
                        Toast.makeText(Toolbar1Activity.this, "item_02", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                }
            });
        }
    }
}